#ifndef _LIST_H_ 
#define _LIST_H_
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#define ARGISNULL       -50001
#define NOTFOUND        -50002

typedef void (*FreeDataFunc)( void *d );
typedef int (*DataCmpFunc)(void *a, void *b);
typedef int (*TraversalFuncWithData)(void *a, void *b);
typedef int (*TraversalFunc)(void *a);


struct _ListHead {
	struct _ListNode *first;
	struct _ListNode *last;
	struct _ListNode *curr;
	int NodeCnt;
} ;
typedef struct _ListHead ListHead;


struct _ListNode {
	struct _ListNode *prev;
	struct _ListNode *next;

	void *data;
	/* 这个字段有点多余 */
	unsigned long datalen;
};
typedef struct _ListNode ListNode;

ListHead* NewListHead();
ListNode* NewListNode();
void FreeListNode(ListNode *node);
void FreeListNodeAndData(ListNode *node, FreeDataFunc func);
void FreeListHead( ListHead *head );
void* FindDataInList( ListHead *head, void *data, DataCmpFunc func);
void* RFindDataInList( ListHead *head, void *data, DataCmpFunc func);

int InsertDataBeTheFirst( ListHead *head, void *data);
int InsertDataBeTheLast( ListHead *head, void *data);

ListNode* FindNodeInList( ListHead *head, void *data, DataCmpFunc func);
int FindDataAndInsertBefore( ListHead *head, void *data, void *inData, DataCmpFunc func );
int FindDataAndInsertAfter( ListHead *head, void *data, void *inData, DataCmpFunc func );

int DeleteListNodeAndData( ListHead *head, ListNode *node, FreeDataFunc freeFunc );
int DeleteListNode( ListHead *head, ListNode* node);

int FindAndDeleteNodeAndDataAll( ListHead *head, void *data, DataCmpFunc cmp, FreeDataFunc freeFunc);
int FindAndDeleteNodeAll( ListHead *head, void *data, DataCmpFunc cmp); 
int FindAndDeleteNodeAndData( ListHead *head, void *data, DataCmpFunc cmp , FreeDataFunc freeFunc);
int FindAndDeleteNode( ListHead *head, void *data, DataCmpFunc cmp );

int TraversalListWithData( ListHead *head, TraversalFuncWithData func, void *data);
int TraversalList( ListHead *head, TraversalFunc func);
/* func必须有值 */
int DamageList( ListHead *head, FreeDataFunc func);
int DamageListNode(ListHead *head, FreeDataFunc freeFunc);
#endif
